
//$(document).ready(function(){
	
/*	$("#addTemp").click(function(){
		window.location.href="/p/biz/tmp/toTemplate";
	})
	
	$("button[name='show']").click(function(){
		 var cpid = $(this).parent().parent().find("td").eq(1).text();
		 window.location.href="/p/biz/tmp/show?id="+ encodeURIComponent(cpid);
	  });*/
	
	/*$("button[name='delete']").click(function(){
		 var cpid = $(this).parent().parent().find("td").eq(1).text();
		window.location.href="/p/biz/tmp/delete?id="+ encodeURIComponent(cpid);
	  });*/
	
//});

var roleManage = function() {
	var _init = function() {
		var table = $('#table_role');
		// var form = $('#form1').serializeObject();
		table
				.dataTable({
					"processing" : true,
					// 开启服务器模式
					serverSide : true,
					"ajax" : {
						url : "/a/biz/tmp"// 列表查询
					// data:
					// {"label":form.label,"templates":form.templates,"createDate1":form.createDate1,"createDate2":form.createDate2}
					},
					"autoWidth" : false,
					"columns" : [
						/*	{
								"width" : "5%",
								"data" : "null",
								"defaultContent" : "<label class=\"mt-checkbox mt-checkbox-single mt-checkbox-outline\"><input type=\"checkbox\" class=\"checkboxes\" value=\"1\"><span></span></label>"
							},*/
							/*{
								"width" : "20%",
								"data" : "wxTemplateId"
							},*/
							{
								"width" : "25%",
								"data" : "name"
							},
							{
								"width" : "25%",
								"data" : "createDate"
							} ],
					"language" : {
						"lengthMenu" : "每页显示 _MENU_ 条记录",
						"emptyTable" : "暂无数据记录",
						"info" : "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
						"zeroRecords" : "没有搜索到匹配记录",
						"infoEmpty" : "",
						"infoFiltered" : "",
						"processing" : ""
					},
					"destroy" : true,
					"pageLength" : 5,
					"order" : [ [ 0, "desc" ] ],
					"columnDefs" : [ {
						"orderable" : false,
						"targets" : [ 0, 1 ]
					}, {
						"targets" : 0,
						"render" : function(data, type, row, meta) {
							return data;
						}
					}/*, {
						"targets" : 1,
						"render" : function(data, type, row, meta) {
							return data;
						}
					}*/, {
						"targets" : 1,
						 "render": function(data, type, row, meta) {
			                    return moment(data).format('YYYY-MM-DD HH:mm');
			                }
					} ],
					"dom" : "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>"
							+ "<t>" + "<'row'<'col-md-5'i><'col-md-7'>p>",
					"fnInitComplete" : function() {
					}
				});
		
		   // 添加
        $('#btn_new_role').click(function(event) {
            $('#modal_new_role').modal('show');
        });
	}

	return {
		init : function() {
			_init();
		}

	};

}();

var roleForm = function() {
	// 新建表单验证
    var validate_role_new = function() {

        var form_new = $('#form_role_new');
        var error_new = $('.alert-danger', form_new);
        var success_new = $('.alert-success', form_new);
        var server_error_new = $('.alert-warning', form_new);

        form_new.validate({
            errorElement: 'span',
            errorClass: 'help-block help-block-error',
            focusInvalid: false,
            ignore: "", // 不验证隐藏input
            rules: {
                name: {
                    required: true
                }/*,
                wxTemplateId: {
                    required: true
                }*/
            },
            messages: {
                name: {
                    required: "请输入模板名称"
                }/*,
                wxTemplateId: {
                    required: "请输入小程序模板ID"
                }*/
            },

            errorPlacement: function(error, element) {
                if (element.parent(".input-group").size() > 0) {
                    error.insertAfter(element.parent(".input-group"));
                } else if (element.attr("data-error-container")) {
                    error.appendTo(element.attr("data-error-container"));
                } else if (element.parents('.radio-list').size() > 0) {
                    error.appendTo(element.parents('.radio-list').attr("data-error-container"));
                } else if (element.parents('.radio-inline').size() > 0) {
                    error.appendTo(element.parents('.radio-inline').attr("data-error-container"));
                } else if (element.parents('.checkbox-list').size() > 0) {
                    error.appendTo(element.parents('.checkbox-list').attr("data-error-container"));
                } else if (element.parents('.checkbox-inline').size() > 0) {
                    error.appendTo(element.parents('.checkbox-inline').attr("data-error-container"));
                } else {
                    error.insertAfter(element);
                }
            },

            invalidHandler: function(event, validator) {
                success_new.hide();
                server_error_new.hide();
                error_new.show();
            },

            highlight: function(element) {
                $(element)
                    .closest('.form-group').addClass('has-error');
            },

            unhighlight: function(element) {
                $(element)
                    .closest('.form-group').removeClass('has-error');
            },

            success: function(label) {
                label
                    .closest('.form-group').removeClass('has-error');
            },

            submitHandler: function(form) {
                success_new.show();
                error_new.hide();
                server_error_new.hide();
                $(form).find('button[type=submit]').attr('disabled', 'disabled');

                var _data = JSON.stringify($(form).serializeObject());
   	          //  checkTmp();
                
                var url= "/a/biz/tmp/check";      
                $.ajax({
                    type: "PUT", 
                    dataType: "json",
                   // async:false, 
                    url:url,
                	contentType:"application/json; charset=utf-8",
                    data: _data, 
                    success: function (data) {
                   	 if(data.status ==1){
                   	   request_role_new(_data, function(res) {
                           // 正确
                           $("#table_role").DataTable().ajax.reload(); // 刷新table
                           $('#modal_new_role').modal('hide');
                           $(form).find('button[type=submit]').removeAttr('disabled');     
                           $('.alert-success').text('');
                           $('.alert-success').removeClass();
                           $('alert-success').attr('class', '');
                           error_new.hide();
                       }, function(err) {
                           //	错误
                           success_new.hide();
                           server_error_new.show();
                           server_error_new.html(err);
                           $(form).find('button[type=submit]').removeAttr('disabled');
                       })
                   		 
                       	 }else{
                       		 alert("该模板存在,请不要重复添加！");
                       		success_new.hide();
                       	    $(form).find('button[type=submit]').removeAttr('disabled');
                       		 return false;
                       	 }
                    },error:function(data){
                   	 alert("改模板存在,请不要重复添加！");
                   	 return false;
                    }
                });
                return false;
            }
        });
    }
    
	return {
		init : function() {
		    validate_role_new();
		}

	};
}();

$(function() {
	roleManage.init();
	roleForm.init();
});

/*function checkTmp(){
    var _data = JSON.stringify($("#form_role_new").serializeObject());
	   //先驗證模板存在不
    var url= "/a/biz/tmp/check";      
    $.ajax({
        type: "PUT", 
        dataType: "json",
       // async:false, 
        url:url,
    	contentType:"application/json; charset=utf-8",
        data: JSON.stringify(_data), 
        success: function (data) {
       	 if(data.status ==1){
           	 }else{
           		 alert("改模板存在,請不要重復添加！");
           		 return false;
           	 }
        },error:function(data){
       	 alert("改模板存在,請不要重復添加！");
       	 return false;
        }
    });
}*/
